package hichecker.university;

import hichecker.controller.Config;

import java.util.HashMap;

import javax.xml.bind.annotation.XmlList;


public class GradingKey {
	public static final int NUM_VALUES = 10;
	private @XmlList float[] key = new float[10];

	public GradingKey() {
		setStandard();
	}

	public GradingKey(GradingKey gradingKey) {
		for (int i = 0; i < NUM_VALUES; i++) {
			key[i] = gradingKey.key[i];
		}
	}

	public void setStandard() {
		for (int i = 0; i < NUM_VALUES; i++)
			key[i] = 1f / 11f * (i + 1);
	}

	public float getPercentage(int index) {
		return key[index];
	}

	public void setPercentage(int index, float value) {
		key[index] = value;
		for (int i = 0; i < index; i++) {
			key[i] = Math.min(key[i], value);
		}
	}

	public float[] getKey() {
		return key;
	}

	public int[] calcGrades(HashMap<Float, Integer> countedPoints, float maxPoints) {
		int[] grades = new int[NUM_VALUES + 1];
		for (float point : countedPoints.keySet()) {
			int i = 0;
			while (i < NUM_VALUES && point >= convertToGrid(key[i] * maxPoints))
				i++;

			grades[i] += countedPoints.get(point);
		}

		return grades;
	}

	public static float convertToGrid(float value) {
		return Math.round(value / Config.MINIMUM_SCORE) * Config.MINIMUM_SCORE;
	}
}
